<?php

namespace app\models;

use Yii;
use app\builder\common\CommonActiveRecord;
use app\builder\contract\AttachmentModelInterface;

/**
 * This is the model class for table "{{%attachment}}".
 *
 * @property int $id
 * @property string $origin_name 文件原名称
 * @property string $type 文件分类，分类名称可按后台菜单名称命名。
 * @property string $bucket 存储空间
 * @property string $save_directory 保存目录
 * @property string $path_prefix 路径前缀
 * @property string $basename 文件名
 * @property int $size 文件大小(字节)
 * @property string $ext 扩展名
 * @property string $mime 文件类型
 * @property string $hash 文件hash值
 * @property string $created_at 创建时间
 * @property string|null $updated_at 更新时间
 */
class Attachment extends CommonActiveRecord implements AttachmentModelInterface
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%attachment}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['bucket', 'save_directory', 'path_prefix', 'basename'], 'required'],
            [['size'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['type'], 'string', 'max' => 100],
            [['bucket', 'save_directory', 'path_prefix', 'mime'], 'string', 'max' => 50],
            [['basename', 'hash', 'origin_name'], 'string', 'max' => 255],
            [['ext'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     * @return array
     */
    public function scenarios()
    {
        $scenario = parent::scenarios(); // TODO: Change the autogenerated stub
        // 场景：新增
        $scenario['add'] = ['bucket', 'save_directory', 'path_prefix', 'basename', 'size', 'type', 'mime', 'hash', 'ext', 'origin_name'];

        return $scenario;
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.admin', 'ID'),
            'origin_name' => Yii::t('app.admin', 'Origin Name'),
            'type' => Yii::t('app.admin', 'Type'),
            'bucket' => Yii::t('app.admin', 'Bucket'),
            'save_directory' => Yii::t('app.admin', 'Save Directory'),
            'path_prefix' => Yii::t('app.admin', 'Path Prefix'),
            'basename' => Yii::t('app.admin', 'Basename'),
            'size' => Yii::t('app.admin', 'Size'),
            'ext' => Yii::t('app.admin', 'Ext'),
            'mime' => Yii::t('app.admin', 'Mime'),
            'hash' => Yii::t('app.admin', 'Hash'),
            'created_at' => Yii::t('app.admin', 'Created At'),
            'updated_at' => Yii::t('app.admin', 'Updated At'),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function saveData(array $data)
    {
        $_this = new static();
        $_this->setScenario('add');

        $_this->load($data);
        if ($_this->validate()) {
            $one = static::findOne(['type' => $data['type'], 'save_directory' => $data['save_directory']]);
            if (empty($one)) {
                $orOne = static::query('id')->where(['type' => $data['type']])->orWhere(['save_directory' => $data['save_directory']])->one();
                if (!empty($orOne)) {
                    $_this->addError('type', t('The classification and save directory are inconsistent', 'app.admin'));
                    return $_this;
                }
            }

            $_this->save(false);
        }

        return $_this;
    }
}
